package com.zxj.databaseframework;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.zxj.databaseframework.bean.User;
import com.zxj.databaseframework.db.BaseDao;
import com.zxj.databaseframework.db.BaseDaoFactory;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    private BaseDao<User> mBaseDao;

    private int age = 10;
    private EditText mEtName;
    private EditText mEtSno;
    private EditText mEtPwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mEtSno = findViewById(R.id.et_sno);
        mEtName = findViewById(R.id.et_name);
        mEtPwd = findViewById(R.id.et_pwd);
        mBaseDao = BaseDaoFactory.getInstance().getDataHelper(UserDao.class,User.class);
        Toast.makeText(this,"创建数据库表成功",Toast.LENGTH_SHORT).show();
    }

    public void onSaveClick(View view){
        mBaseDao = BaseDaoFactory.getInstance().getDataHelper(UserDao.class,User.class);
        Toast.makeText(this,"创建数据库表成功",Toast.LENGTH_SHORT).show();
    }

    public void onInsertClick(View view){
        String stuNo = mEtSno.getText().toString().trim();
        String name = mEtName.getText().toString().trim();
        String password = mEtPwd.getText().toString().trim();
        mBaseDao.insert(new User(stuNo,age++,name,password));
        Toast.makeText(this,"插入成功",Toast.LENGTH_SHORT).show();
    }

    public void onUpdateClick(View view){
        User user = new User();
        user.setPassword("321");
        User where = new User();
        where.setName("zxj");
        mBaseDao.update(user,where);
        Toast.makeText(this,"更新成功",Toast.LENGTH_SHORT).show();
    }

    public void onDeleteClick(View view){
        User user = new User();
        user.setStuId("774");
        mBaseDao.delete(user);
        Toast.makeText(this,"删除成功",Toast.LENGTH_SHORT).show();
    }

    public void onSelectClick(View view){
       /* User where = new User();
        where.setName("zxj");
        List<User> users = mBaseDao.query(where);
        */

       String sql = "select * from t_user where name = 'zxj'";
        List<User> users = mBaseDao.query(sql, User.class);
        Log.e("zxj","查询到 "+users.size()+" 条数据");
        for (User user : users) {
            Log.e("zxj",user.toString());
        }
    }

}
